﻿@page "/admin/posts"
@page "/admin/posts/{page:int}"

<AdminLayout>
    @if (posts == null)
    {
        <Loading />
    }
    else
    {
        <div class="post-wrap archive">
            <NavLink style="float:right" href="/admin/post"><h3>📝~~~ 新增文章 ~~~📝</h3></NavLink>
            @if (posts.Success && posts.Result.Item.Any())
            {
                @foreach (var item in posts.Result.Item)
                {
                    <h3>@item.Year</h3>
                    @foreach (var post in item.Posts)
                    {
                        <article class="archive-item">
                            <NavLink title="❌删除" @onclick="@(async () => await DeleteAsync(post.Id))">❌</NavLink>
                            <NavLink title="📝编辑" @onclick="@(async () => await Common.NavigateTo($"/admin/post/{post.Id}"))">📝</NavLink>
                            <NavLink target="_blank" class="archive-item-link" href="@("/post" + post.Url)">@post.Title</NavLink>
                            <span class="archive-item-date">@post.CreationTime</span>
                        </article>
                    }
                }
                <nav class="pagination">
                    @for (int i = 1; i <= TotalPage; i++)
                    {
                        var _page = i;

                        if (page == _page)
                        {
                            <span class="page-number current">@_page</span>
                        }
                        else
                        {
                            <a class="page-number" @onclick="@(() => RenderPage(_page))" href="/admin/posts/@_page">@_page</a>
                        }
                    }
                </nav>
            }
            else
            {
                <ErrorTip />
            }
        </div>
    }
</AdminLayout>

@code {
    /// <summary>
    /// 当前页码
    /// </summary>
    [Parameter]
    public int? page { get; set; }

    /// <summary>
    /// 限制条数
    /// </summary>
    private int Limit = 15;

    /// <summary>
    /// 总页码
    /// </summary>
    private int TotalPage;

    /// <summary>
    /// 文章列表数据
    /// </summary>
    private ServiceResult<PagedList<QueryPostForAdminDto>> posts;

    /// <summary>
    /// 初始化
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        var token = await Common.GetStorageAsync("token");
        Http.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");

        // 设置默认值
        page = page.HasValue ? page : 1;

        await RenderPage(page);
    }

    /// <summary>
    /// 点击页码重新渲染数据
    /// </summary>
    /// <param name="page"></param>
    /// <returns></returns>
    private async Task RenderPage(int? page)
    {
        // 获取数据
        posts = await Http.GetFromJsonAsync<ServiceResult<PagedList<QueryPostForAdminDto>>>($"/blog/admin/posts?page={page}&limit={Limit}");

        if (!posts.Success)
            await Common.NavigateTo("/");

        // 计算总页码
        TotalPage = (int)Math.Ceiling((posts.Result.Total / (double)Limit));
    }

    /// <summary>
    /// 删除文章
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    private async Task DeleteAsync(int id)
    {
        // 弹窗确认
        bool confirmed = await Common.InvokeAsync<bool>("confirm", "\n💥💢真的要干掉这篇该死的文章吗💢💥");

        if (confirmed)
        {
            var response = await Http.DeleteAsync($"/blog/post?id={id}");

            var result = await response.Content.ReadFromJsonAsync<ServiceResult>();

            if (result.Success)
            {
                await RenderPage(page);
            }
        }
    }
}